package com.xbongbong.pro.message.pushset;/*/*/

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.message.domain.entity.PushSetEntity;
import com.xbongbong.pro.message.base.AbstractPushSet;
import com.xbongbong.pro.message.constant.PushSetConstant;
import com.xbongbong.pro.message.enums.PushObjectTypeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.model.PushSetModel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 客户合同消息设置
 *
 * @author GuoJun.HU
 * @since V1.0
 * @date 2019/2/19 10:41
 */
public class CustomerContactPushSet extends AbstractPushSet {
    @Override
    public List<PushSetEntity> getPushSet(String corpid, List<String> attrList, PushSetModel pushSetModel, Long templateId) {
        return null;
    }

    private static Integer oneDay = 86400;

    private String pushObject;
    private String pushTimeObject;

    public String getPushObject() {
        return pushObject;
    }

    public void setPushObject(String pushObject) {
        this.pushObject = pushObject;
    }

    public String getPushTimeObject() {
        return pushTimeObject;
    }

    public void setPushTimeObject(String pushTimeObject) {
        this.pushTimeObject = pushTimeObject;
    }

    @Override
    public PushSetEntity toPushSetEntity(String pushObject, String pushTime, String attrEntity) {
        setPushObject(pushObject);
        setPushTimeObject(pushTime);

        PushSetEntity pushSetEntity = new PushSetEntity();

        pushSetEntity.setAddTime(addTime);
        pushSetEntity.setAttrEntity(attrEntity);
        pushSetEntity.setCorpid(corpid);
        pushSetEntity.setDel(del);
        pushSetEntity.setEnable(enable);
        pushSetEntity.setId(id);
        pushSetEntity.setPushObject(pushObject);
        pushSetEntity.setPushTime(pushTime);
        pushSetEntity.setRefEntity(refEntity);
        pushSetEntity.setSubType(subType);
        pushSetEntity.setType(type);
        pushSetEntity.setUpdateTime(updateTime);

        return pushSetEntity;
    }

    @Override
    public PushSetEntity getPushSet(String corpid, Integer subType, PushSetModel pushSetModel) {
        Integer type = PushTypeEnum.CONTACT_PUSH.getCode();
        Map<String, Object> param = new HashMap<>(16);
        param.put("corpid", corpid);
        param.put("type", type);
        param.put("subType", subType);
        List<PushSetEntity> pushSetList = pushSetModel.findEntitys(param);

        PushSetEntity pushSetEntity;
        if (pushSetList.isEmpty()) {
            pushSetEntity = new PushSetEntity();
            pushSetEntity.setType(type);
            pushSetEntity.setSubType(subType);

            JSONObject pushObject = new JSONObject();
            if (Objects.equals(PushTypeEnum.CONTACT_NEW.getSubCode(), subType)) {
                pushSetEntity.setEnable(0);

                pushSetEntity.setTitle(PushTypeEnum.CONTACT_NEW.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.CONTACT_NEW));
                //type
                JSONArray typeArray = new JSONArray();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name", PushObjectTypeEnum.MAINUSER.getName());
                jsonObject.put("enable", 1);
                jsonObject.put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
                typeArray.add(jsonObject);

                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("name", PushObjectTypeEnum.COLLABORATOR.getName());
                jsonObject1.put("enable", 1);
                jsonObject1.put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
                typeArray.add(jsonObject1);
                pushObject.put("type", typeArray);
                //manager
                JSONObject managerObject = new JSONObject();
                managerObject.put("enable", 0);
                managerObject.put("value", new ArrayList<>());
                pushObject.put("manager", managerObject);
                //dep
                JSONObject depObject = new JSONObject();
                depObject.put("enable", 0);
                depObject.put("value", new ArrayList<>());
                pushObject.put("dep", depObject);
                //role
                JSONObject roleObject = new JSONObject();
                roleObject.put("enable", 0);
                roleObject.put("value", new ArrayList<>());
                pushObject.put("role", roleObject);
                //user
                JSONObject userObject = new JSONObject();
                userObject.put("enable", 0);
                userObject.put("value", new ArrayList<>());
                pushObject.put("user", userObject);


            }
//            else if (Objects.equals(PushTypeEnum.CONTACT_BIRTHDAY.getSubCode(), subType)) {
//                pushSetEntity.setEnable(0);
//
//                pushSetEntity.setTitle(PushTypeEnum.CONTACT_BIRTHDAY.getName());
//                pushSetEntity.setContent(PushSetConstant.CONTACT_BIRTHDAY);
//                //type
//                JSONArray typeArray = new JSONArray();
//                JSONObject jsonObject = new JSONObject();
//                jsonObject.put("name", PushObjectTypeEnum.MAINUSER.getName());
//                jsonObject.put("enable", 1);
//                jsonObject.put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
//                typeArray.add(jsonObject);
//
//                JSONObject jsonObject1 = new JSONObject();
//                jsonObject1.put("name", PushObjectTypeEnum.COLLABORATOR.getName());
//                jsonObject1.put("enable", 0);
//                jsonObject1.put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
//                typeArray.add(jsonObject1);
//                pushObject.put("type", typeArray);
//                //role
//                JSONObject roleObject = new JSONObject();
//                roleObject.put("enable", 0);
//                roleObject.put("value", new ArrayList<>());
//                pushObject.put("role", roleObject);
//                //pushTime
//                JSONObject pushTimeObject = new JSONObject();
//                pushTimeObject.put("period", CustomerContactPushSet.oneDay);
//                pushTimeObject.put("timeType", PushTimeTypeEnum.ADVANCE.getCode());
//                pushTimeObject.put("loop", 0);
//                pushTimeObject.put("time", 0);
//                pushTimeObject.put("type", 1);
//
//                JSONArray timeArray = new JSONArray();
//                JSONObject timeObject = new JSONObject();
//                timeObject.put("id", 1);
//                timeObject.put("name", "提前");
//                timeArray.add(timeObject);
//                JSONObject timeObject2 = new JSONObject();
//                timeObject2.put("id", 3);
//                timeObject2.put("name", "当天");
//                timeArray.add(timeObject2);
//                pushTimeObject.put("timeArray", timeArray);
//
//                pushSetEntity.setPushTime(pushTimeObject.toJSONString());
//            }

            pushSetEntity.setPushObject(pushObject.toJSONString());
            pushSetList.add(pushSetEntity);
        } else {
            pushSetEntity = pushSetList.get(0);

            if (Objects.equals(PushTypeEnum.CONTACT_NEW.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.CONTACT_NEW.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.CONTACT_NEW));

            }
//            else if (Objects.equals(PushTypeEnum.CONTACT_BIRTHDAY.getSubCode(), subType)) {
//                pushSetEntity.setTitle(PushTypeEnum.CONTACT_BIRTHDAY.getName());
//                pushSetEntity.setContent(PushSetConstant.CONTACT_BIRTHDAY);
//
//            }
        }
        return pushSetEntity;
    }

    @Override
    public PushSetEntity getPushSet(String corpid, String attr, PushSetModel pushSetModel) {
        return null;
    }

    @Override
    public JSONObject getTimeJson(JSONObject jsonObject) {
        return null;
    }
}
